// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   MathHelper.java

package org.achartengine.util;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

public class MathHelper
{

	public static final double NULL_VALUE = 1.7976931348623157E+308D;
	private static final NumberFormat FORMAT = NumberFormat.getNumberInstance();

	private MathHelper()
	{
	}

	public static double[] minmax(List list)
	{
		if (list.size() == 0)
			return new double[2];
		double d = ((Double)list.get(0)).doubleValue();
		double d1 = d;
		int i = list.size();
		for (int j = 1; j < i; j++)
		{
			double d2 = ((Double)list.get(j)).doubleValue();
			d = Math.min(d, d2);
			d1 = Math.max(d1, d2);
		}

		return (new double[] {
			d, d1
		});
	}

	public static List getLabels(double d, double d1, int i)
	{
		FORMAT.setMaximumFractionDigits(5);
		ArrayList arraylist = new ArrayList();
		double ad[] = computeLabels(d, d1, i);
		int j = 1 + (int)((ad[1] - ad[0]) / ad[2]);
		for (int k = 0; k < j; k++)
		{
			double d2 = ad[0] + (double)k * ad[2];
			try
			{
				d2 = FORMAT.parse(FORMAT.format(d2)).doubleValue();
			}
			catch (ParseException parseexception) { }
			arraylist.add(Double.valueOf(d2));
		}

		return arraylist;
	}

	private static double[] computeLabels(double d, double d1, int i)
	{
		if (Math.abs(d - d1) < 1.0000000116860974E-007D)
			return (new double[] {
				d, d, 0.0D
			});
		double d2 = d;
		double d3 = d1;
		boolean flag = false;
		if (d2 > d3)
		{
			flag = true;
			double d4 = d2;
			d2 = d3;
			d3 = d4;
		}
		double d5 = roundUp(Math.abs(d2 - d3) / (double)i);
		double d6 = d5 * Math.ceil(d2 / d5);
		double d7 = d5 * Math.floor(d3 / d5);
		if (flag)
			return (new double[] {
				d7, d6, -1D * d5
			});
		else
			return (new double[] {
				d6, d7, d5
			});
	}

	private static double roundUp(double d)
	{
		int i = (int)Math.floor(Math.log10(d));
		double d1 = d * Math.pow(10D, -i);
		if (d1 > 5D)
			d1 = 10D;
		else
		if (d1 > 2D)
			d1 = 5D;
		else
		if (d1 > 1.0D)
			d1 = 2D;
		d1 *= Math.pow(10D, i);
		return d1;
	}

	public static float[] getFloats(List list)
	{
		int i = list.size();
		float af[] = new float[i];
		for (int j = 0; j < i; j++)
			af[j] = ((Float)list.get(j)).floatValue();

		return af;
	}

}
